package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-12-21
 * Time:16:00
 */
@WebServlet("/index")
//登录成功后，跳转到的主页
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首页中也先获取 session ，此处的session 正是刚才登录页的时候，登录成功的逻辑中创建出来的
        //此处 参数写作 false 即可，表示不新键，如果不存在，就返回null就算了
        HttpSession session = req.getSession(false);
        if (session == null){
            resp.setStatus(403);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("您尚未登录，不能访问主页！");
            return;
        }
        String username = (String) session.getAttribute("username");
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("欢迎来到主页" + username);
    }
}
